using System.Collections.Generic;
using BCW.Friends;
using UnityEngine;

public class FriendsInSelectModeUI : MonoBehaviour
{
	[SerializeField]
	private Transform contentTransform;

	[SerializeField]
	private UIGrid contentGrid;

	[SerializeField]
	private FriendsInSelectModePanel examplePlayer;

	[SerializeField]
	private UIBasicSprite spriteFriendsPanel;

	private UIBasicSprite _spriteFriendsButton;

	private List<FriendsInSelectModePanel> _freindsList = new List<FriendsInSelectModePanel>();

	public UIBasicSprite SpriteFriendsButton
	{
		get
		{
			return _spriteFriendsButton;
		}
		set
		{
			_spriteFriendsButton = value;
		}
	}

	private void OnEnable()
	{
		ClearList();
		Init();
		FriendsController.instance.UpdatePlayersDB(null);
	}

	private void Update()
	{
		CheckClickInRect checkClickInRect = new CheckClickInRect();
		if (Input.GetMouseButtonDown(0) && !checkClickInRect.InRange(spriteFriendsPanel, Input.mousePosition) && !checkClickInRect.InRange(_spriteFriendsButton, Input.mousePosition))
		{
			base.gameObject.SetActive(false);
		}
		Touch[] touches = Input.touches;
		for (int i = 0; i < touches.Length; i++)
		{
			Touch touch = touches[i];
			if (touch.phase == TouchPhase.Began && !checkClickInRect.InRange(spriteFriendsPanel, touch.position) && !checkClickInRect.InRange(_spriteFriendsButton, touch.position))
			{
				base.gameObject.SetActive(false);
			}
		}
	}

	private void Init()
	{
		int num = 0;
		List<string> list = new List<string>();
		foreach (PlayerInfo fiends in FriendsController.instance.GetFiendsList())
		{
			FriendsShowInListCheckerInterface checker = FriendsShowInListFactory.getChecker(FriendsUIChat.FriendsUIChatTypeUsing.firstLevel);
			if (checker.check(fiends))
			{
				FriendsInSelectModePanel friendsInSelectModePanel = Object.Instantiate(examplePlayer);
				friendsInSelectModePanel.transform.SetParent(contentTransform);
				friendsInSelectModePanel.transform.localScale = Vector3.one;
				friendsInSelectModePanel.SetName(fiends.name);
				friendsInSelectModePanel.SetSkin(fiends.skinId);
				friendsInSelectModePanel.IdPlayer = fiends.playerId;
				friendsInSelectModePanel.gameObject.SetActive(true);
				friendsInSelectModePanel.RefreshMark();
				num++;
				_freindsList.Add(friendsInSelectModePanel);
				list.Add(fiends.playerId);
			}
		}
		if ((bool)FriendsController.instance && FriendsController.instance.IdsFrinedsInviteToBattle.Count > 0)
		{
			foreach (string item in FriendsController.instance.IdsFrinedsInviteToBattle)
			{
				if (!list.Contains(item))
				{
					FriendsController.instance.IdsFrinedsInviteToBattle.Remove(item);
				}
			}
		}
		contentGrid.repositionNow = true;
	}

	private void ClearList()
	{
		foreach (FriendsInSelectModePanel freinds in _freindsList)
		{
			Object.Destroy(freinds.gameObject);
		}
		_freindsList.Clear();
	}
}
